<?php

/**
 *  栏目管理
 */
class CategoryAction extends CommonAction {

    /**
     * 栏目列表
     */
    public function index() {
        $category = M('Category')->order('sort asc,id asc')->select();
        if ($category) {
            $Model = M('Model');
            foreach ($category as $key => $value) {
                $category[$key]['model_name'] = $Model->getFieldById($value['model_id'], 'name');
            }
        }
        load('extend');  //加载函数库
        $this->assign('category', $this->_getCategoryList(list_to_tree($category)));
        $this->display('index');
    }

    /**
     * 新增栏目
     */
    public function add() {
        if ($_POST) {
            if (!empty($_POST['name'])) {
                if (M('Category')->add($_POST)) {
                    $this->assign('notice', array('status' => 'success', 'message' => '新增栏目成功！'));
                }
            } else {
                $this->assign('notice', array('status' => 'error', 'message' => '请填写栏目名称'));
            }
        }
        $this->assign('categorySelect', $this->_getCategorySelect($this->_getCategoryTree()));
        $this->assign('model', M('Model')->select());   //模型
        $this->display();
    }

    /**
     * 修改
     */
    public function edit() {
        $id = $_GET['id'];
        if ($_POST) {
            if (!empty($_POST['name'])) {
                if (M('Category')->where('id = ' . $id)->save($_POST)) {
                    $this->assign('notice', array('status' => 'success', 'message' => '修改栏目成功！'));
                }
            } else {
                $this->assign('notice', array('status' => 'error', 'message' => '请填写栏目名称'));
            }
        }
        $this->assign('categorySelect', $this->_getCategorySelect($this->_getCategoryTree($id)));
        $this->assign('category', M('Category')->find($id));
        $this->assign('model', M('Model')->select());   //模型
        $this->display();
    }

    /**
     * 删除
     */
    public function delete() {
        $id = $_GET['id'];
        $Category = M('Category');
        
        if ($Category->where('pid = ' . $id)->find()) {
            $this->assign('notice', array('status' => 'error', 'message' => '操作失败！请先删除该栏目下的所有子栏目！！！'));
        } else {
            $Category->where('id = ' . $id)->delete();
        }
        
        $this->index();
    }

    /**
     * 获取分类树
     * $id 排除这个ID
     */
    private function _getCategoryTree($id = '') {
        if ($id) {
            $condition = 'id != ' . $id;
        }
        $category = M('Category')->where($condition)->order('sort asc')->select();
        load('extend');  //加载函数库
        return list_to_tree($category);
    }

    /**
     * 栏目列表页
     */
    private function _getCategoryList($tree, $pid = 0, $level = 0) {
        $space = str_repeat('&nbsp;└ ', $level);
        $level++;
        foreach ($tree as $value) {
            if ($value['pid'] == $pid) {
                $html .= '<tr>';
                $html .= '<td> ' . $space . ' ' . $value['id'] . '</td>';
                $html .= '<td><a href="__URL__/edit?id=' . $value['id'] . '" title="查看">' . $value['name'] . '</a></td>';
                $model_name = $value['isOutUrl'] == 1? '外部链接' : $value['model_name'];
                $html .= '<td>' . $model_name . '</td>';
                $html .= '<td>' . $value['sort'] . '</td>';
                $html .= '<td><a href="__URL__/edit?id=' . $value['id'] . '" title="修改"><img src="../Public/images/icons/pencil.png" alt="修改" /></a> <a href="__URL__/delete?id=' . $value['id'] . '" title="删除" onclick="return confirm(\'确定删除该栏目？该操作不可恢复。\');"><img src="../Public/images/icons/cross.png" alt="删除" /></a>';
                $html .= '</tr>';
            }
            if (isset($value['_child'])) {
                $html .= $this->_getCategoryList($value['_child'], $value['id'], $level);
            }
            
        }
         
        return $html;
    }

    /**
     * 获取无限分类select
     */
    private function _getCategorySelect($tree, $pid = 0, $level = 0) {
        $space = str_repeat('&nbsp;└ ', $level);
        $level++;
        foreach ($tree as $value) {
            if ($value['pid'] == $pid) {
                $html .= '<option value="' . $value['id'] . '">' . $space . $value['name'] . '</option>';
            }
            if (isset($value['_child'])) {
                $html .= $this->_getCategorySelect($value['_child'], $value['id'], $level);
            }
        }
        return $html;
    }

}